---@class TBS.Skill:TBS.SkillBase
---@field New fun():TBS.Skill
local Skill = TBS.CreateClass("Skill","SkillBase")
function Skill:OnCreate(skillId)
    TBS.SkillBase.OnCreate(self,skillId)
    self.isHit = false
    ---@type TBS.Player[]
    self.targetList = {}
    self.skillAudio = ""    --技能音效
    self.hitAudio = ""      --击中音效
    self.subSkillIndex = 1
    self.hasFrameEvent = false
    self:InitCfg()
end
function Skill:OnDestroy()
    TBS.SkillBase.OnDestroy(self)
    TablePool.ReleaseDeeper(self.targetList,false)
    self.targetList = {}
end
function Skill:Set(casterId,uniqueId)
    self.casterId = casterId
    self.uniqueId = uniqueId
    self.subSkillIndex = 1
    self:ClearTarget()
end
function Skill:HasEndTime()
    return self.maxTime > 0
end
function Skill:InitCfg()
    local cfg = dataTable.getDataTableByStringId("t_collocationskill",self.skillId)
    local showCfg = dataTable.getDataTableByStringId("t_collocationskillshow",self.skillId)
    if not cfg then
        logError("找不到t_collocationskill"..self.skillId)
        return
    end
    if not showCfg then
        logError("找不到t_collocationskillshow"..self.skillId)
        return
    end
    self.skillAudio = showCfg.f_SkillAudio
    self.hitAudio = showCfg.f_HitAudio
    self.maxTime = cfg.f_GuideTime/1000
    --self.initialCD = cfg.f_InitialCD
    --self.maxCd = cfg.f_SkillCD
    --self.curCd = self.initialCD

    --施法特效
    if showCfg.f_Release ~= "" then
        local delay = showCfg.f_Release[1]/1000
        local name = "Prefabs/Skill/"..showCfg.f_Release[2]
        local action = showCfg.f_Release[3]
        local node = TBS.EffectNode.New(self,delay)
        node:Set(name,action,0.5,"")
        if showCfg.f_Coordinate1 ~= "" then
            local x,y = TBS.Battle.FGUIToScenePoint(showCfg.f_Coordinate1[1],showCfg.f_Coordinate1[2])
            node:SetOffset(x,y,TBS.Config.EffectPosType.caster)
        end
        if showCfg.f_ReleaseSize ~= 0 then
            node:SetScale(showCfg.f_ReleaseSize/10000)
        end
        self:AddNode(node)
    end
    --施法特效（组员都生成）
    if showCfg.f_ReleaseHero ~= "" then
        local delay = showCfg.f_ReleaseHero[1]/1000
        local name = "Prefabs/Skill/"..showCfg.f_ReleaseHero[2]
        local action = showCfg.f_ReleaseHero[3]
        local node = TBS.EffectNode.New(self,delay)
        node:Set(name,action,0.5,"")
        node:SetProduceByTeamAllHeroAndFollow(true)
        node:SetAudio(self.skillAudio)
        if showCfg.f_ReleaseSize ~= 0 then
            node:SetScale(showCfg.f_ReleaseSize/10000)
        end
        self:AddNode(node)
    end
    --随机飞行特效(子弹)
    if showCfg.f_Aggregate ~= "" then
        local node = TBS.EffectNode.New(self,0)
        local during = showCfg.f_FlyFDuringTime/1000
        if during <= 0 then
            logError("[error]飞行特效时间f_FlyFDuringTime 不能为 "..during)
            during = 0.3
        end
        node:Set(showCfg.f_Aggregate[1],"",during,"")
        node:SetDelayDestroyTime(showCfg.f_FlyDestroy/1000)
        node:SetRandomRes(showCfg.f_Aggregate)
        node:SetBullet(true,0,TBS.Config.TargetType.target)
        self:AddNode(node)
    else
        --飞行特效(子弹)
        if showCfg.f_FlyFResource ~= "" then
            local delay = showCfg.f_FlyFResource[1]/1000
            local name = "Prefabs/Skill/"..showCfg.f_FlyFResource[2]
            local action = showCfg.f_FlyFResource[3]
            local node = TBS.EffectNode.New(self,delay)
            local during = showCfg.f_FlyFDuringTime/1000
            if during <= 0 then
                logError("[error]飞行特效时间f_FlyFDuringTime 不能为 "..during)
                during = 0.1
            end
            node:Set(name,action,during,"")
            local x = 0
            local y = 0
            if showCfg.f_FlyFPosition ~= "" then
                x = showCfg.f_FlyFPosition[1]
                y = showCfg.f_FlyFPosition[2]
            end
            node:SetOffset(x,y,TBS.Config.EffectPosType.caster)
            node:SetDelayDestroyTime(showCfg.f_FlyDestroy/1000)
            node:SetBullet(true,0,TBS.Config.TargetType.target)
            self:AddNode(node)
        else
            --攻击特效(和子弹互斥)
            ---@type TBS.EffectNode
            local node
            local action
            if showCfg.f_Attack ~= "" then
                local delay = showCfg.f_Attack[1]/1000
                local name = "Prefabs/Skill/"..showCfg.f_Attack[2]
                action = showCfg.f_Attack[3]
                node = TBS.EffectNode.New(self,delay)
                node = TBS.EffectNode.New(self,delay)
                node:Set(name,action,0.5,"")
                if showCfg.f_AttackSize ~= 0 then
                    node:SetScale(showCfg.f_AttackSize/10000)
                end
                if showCfg.f_AttackRotate ~= "" then
                    node:SetRandomRotate(showCfg.f_AttackRotate[1],showCfg.f_AttackRotate[2])
                end
                if showCfg.f_AttackPos ~= "" then
                    local p = showCfg.f_AttackPos
                    node:SetRandomBornPos(p[1],p[2],p[3],p[4])
                end
                node:SetEffectLoadDoneCallback(self.LoadEffectAttackCallback,self)
                if showCfg.f_DrawingID ~= "" then
                    node:SetUltimateDrawing("Model/"..showCfg.f_DrawingID,showCfg.f_DrawingMove)
                end
                local x = 0
                local y = 0
                if showCfg.f_SceneCoordinate ~= "" then --场景特效
                    x = showCfg.f_SceneCoordinate[1]
                    y = showCfg.f_SceneCoordinate[2]
                else
                    if showCfg.f_Coordinate2 ~= "" then
                        x = showCfg.f_Coordinate2[1]
                        y = showCfg.f_Coordinate2[2]
                    end
                end
                node:SetOffset(x,y,showCfg.f_Target)
                node:SetAudio(self.skillAudio)
                self:AddNode(node)
            end
            --帧事件
            local eventNode = TBS.AnimationEventNode.New(self,showCfg.f_HitTime/1000)
            if showCfg.f_IncidentTakeEffect == 0 and node and action ~= nil  then
                eventNode:Set(true,node)
                self.hasFrameEvent = true
            end
            eventNode:SetAudio(self.hitAudio)
            self:AddNode(eventNode)
        end
    end

    --受击特效
    if showCfg.f_Hit ~= "" then
        local delay = showCfg.f_Hit[1]/1000
        local name = "Prefabs/Skill/"..showCfg.f_Hit[2]
        local action = showCfg.f_Hit[3]
        local node = TBS.EffectNode.New(self,delay)
        if showCfg.f_Hit[1] == -1 then
            node:ExecuteForAfterDamage()
        end
        node:Set(name,action,0.5,"")
        if showCfg.f_HitSize ~= 0 then
            node:SetScale(showCfg.f_HitSize/10000)
        end
        local x = 0
        local y = 0
        if showCfg.f_Coordinate3 ~= "" then
            x = showCfg.f_Coordinate3[1]--TBS.Battle.FGUIToScenePoint(showCfg.f_Coordinate3[1],showCfg.f_Coordinate3[2])
            y = showCfg.f_Coordinate3[2]
        end
        node:SetOffset(x,y,TBS.Config.EffectPosType.fixPos)
        node:SetAudio(self.hitAudio)
        self:AddNode(node)
    end
    if showCfg.f_Shock ~= 0 then
        local node = TBS.ShakeNode.New(self,showCfg.f_Shock/1000)
        if showCfg.f_Shock == -1 then
            node:ExecuteForAfterDamage()
        end
        self:AddNode(node)
    end
end
--
function Skill:LoadEffectAttackCallback()
    self:DoSkillCallback(TBS.Config.SkillCallbackType.attackEffectLoadDone)
end
function Skill:DoSkill()
    local flag = TBS.SkillBase.DoSkill(self)
    --if flag then
        --self:StartCoolDown(self.maxCd)
    --end
    return flag
end
---@return TBS.Player
function Skill:GetCaster()
    return TBS.ActorMgr.GetActor(self.casterId)
end
function Skill:EndSkill()
    self.isInCd = false
    table.clear(self.targetList)
    self:OnSkillEnd()
end
function Skill:GetSubSkillIndex()
    return self.subSkillIndex
end
function Skill:InCreaseSubSkillIndex()
    self.subSkillIndex = self.subSkillIndex + 1
end
function Skill:Update(dt)
    TBS.SkillBase.Update(self,dt)
end
function Skill:HitEmpty()
    local hitContext = self:CreateHitContext(0,0,0)
    self:DoSkillCallback(TBS.Config.SkillCallbackType.hitBefore,hitContext)
    self:DoSkillCallback(TBS.Config.SkillCallbackType.hitAfter,hitContext)
end
---@param target TBS.Player
function Skill:Hit(target,hitPosX,hitPosY)
    if not target then
        logError("hit 无目标")
        return
    end
    self.isHit = true
    local targetId = target and target:GetId() or 0
    local hitContext = self:CreateHitContext(targetId,hitPosX,hitPosY)
    self:DoSkillCallback(TBS.Config.SkillCallbackType.hitBefore,hitContext)
    local damage = hitContext.damage
    if target then
        target:Hurt(damage,hitContext.damageType,self:GetCaster(),self.skillId)
        hitContext.eventFinish = false
        self:DoSkillCallback(TBS.Config.SkillCallbackType.hitAfter,hitContext)
    end
end
function Skill:ClearTarget()
    table.clear(self.targetList)
end
function Skill:SetTarget(id)
    table.clear(self.targetList)
    self.targetList[id] = TBS.ActorMgr.GetActor(id)
end
function Skill:AddTarget(id)
    self.targetList[id] = TBS.ActorMgr.GetActor(id)
    --local target = TBS.ActorMgr.GetActor(id)
    --table.insert(self.targetList,target)
end
--回合开始
function Skill:TurnStart()
    self:UpdateCoolDown(1)
    table.clear(self.targetList)
    table.clear(self.beforeDamageCallbackList)
    table.clear(self.afterDamageCallbackList)
end
---技能初始化
function Skill:OnSkillInit()
    TBS.SkillBase.OnSkillInit(self)
    self.isHit = false
    self.subSkillIndex = 1
end
---技能结束
--function Skill:OnSkillEnd()
    --TBS.SkillBase.OnSkillEnd(self)
    --self:GetCaster():EndSkill(self.uniqueId)
--end